package com.invest;

import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.processor.PageProcessor;

public class WebMagicExample implements PageProcessor {
    private Site site = Site.me().setRetryTimes(3).setSleepTime(1000);

    @Override
    public void process(Page page) {
        // 解析网页内容
        String title = page.getHtml().xpath("//title/text()").get();
        System.out.println("网页标题: " + title);

        // 提取链接并加入到待爬取队列中
        page.addTargetRequests(page.getHtml().links().regex("https://example\\.com/.*").all());
    }

    @Override
    public Site getSite() {
        return site;
    }

    public static void main(String[] args) {
        Spider.create(new WebMagicExample())
                .addUrl("https://example.com")
                .run();
    }
}
